package com.tianji.learning.mq;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.service.ILearningLessonService;
import com.tianji.learning.service.impl.LearningLessonServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Snow
 * @version 1.0
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LessonChangeListener {

   final ILearningLessonService learningLessonService;

    @RabbitListener(bindings =@QueueBinding(value = @Queue(value = "learning.lesson.pay.queue",durable = "true")
    ,exchange = @Exchange(value = MqConstants.Exchange.ORDER_EXCHANGE,type = ExchangeTypes.TOPIC)
    ,key = MqConstants.Key.ORDER_PAY_KEY))
    public void onmSG(OrderBasicDTO orderBasicDTO){
        log.info("LessonChangeListener 接收到了消息 用户{} 添加课程{}",orderBasicDTO.getUserId(),orderBasicDTO.getCourseIds());
        ///校验
        if (orderBasicDTO.getUserId() == null||orderBasicDTO.getOrderId() == null
            ||orderBasicDTO.getCourseIds() == null || CollUtils.isEmpty(orderBasicDTO.getCourseIds())
        ){
            return;
        }
        //调用Service保存课程到课表
        learningLessonService.addUserLessons(orderBasicDTO.getUserId(),orderBasicDTO.getCourseIds());

    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "learning.lesson.refund.queue",durable = "true")
            ,exchange = @Exchange(value = MqConstants.Exchange.ORDER_EXCHANGE,type = ExchangeTypes.TOPIC)
            ,key = MqConstants.Key.ORDER_REFUND_KEY))
    public void onRefundMSG(OrderBasicDTO orderBasicDTO){
        log.info("LessonChangeListener 接收到了消息 用户{} 添加课程{}",orderBasicDTO.getUserId(),orderBasicDTO.getCourseIds());
        ///校验
        if (orderBasicDTO.getUserId() == null||orderBasicDTO.getOrderId() == null
                ||orderBasicDTO.getCourseIds() == null || CollUtils.isEmpty(orderBasicDTO.getCourseIds())
        ){
            return;
        }
        //调用Service删除课程
        learningLessonService.deleteCourse(orderBasicDTO);
    }


}
